<?php
/* wwwschool project
Copyright (C) 2009  Phillip Aldridge
Email : info@imi21.com
Web site http://www.imi21.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/**
 *
 *  wwwschool project
 *  File : WS3database.php
 */  
 
 class WS3_Database {
 private $lid = 0;     
 private $qid = 0;		
 private $row;	
 private $count = 0;			
 private $record = array();	
 private $error = "";
 private $errno = "";		
 
   function __construct() {
   }
   
   function __destruct() {
   }
   
  // Connects to DB and returns DB lid 
  private function connect() { 
    if ($this->lid == 0) {
      $this->lid = mysql_pconnect(DB_HOST,DB_USER,DB_PASSWORD); 
      if (!$this->lid) {
	       $this->halt("Database sever is down.");
	       exit;
      } 
      
      if (!mysql_select_db(DB_NAME,$this->lid)) {
      	$this->halt("Cannot select database ".DB_NAME);
      	return false;
      }
    }
    return $this->lid;
  }
  
  // Runs query and sets up the query id for the class.
  public function query($q) {
 if (W3DEBUG) array_push($GLOBALS['W3DEBUG'],$q);   
    if (empty($q))
      return false;
    
    if (!$this->connect()) {
      return false;
    }
    
    if ($this->qid) {
      @mysql_free_result($this->qid);
      $this->qid = 0;
    }
    
    $this->qid = mysql_query($q, $this->lid);
    $this->row   = 0;
    $this->errno = mysql_errno();
    $this->error = mysql_error();
    if (!$this->qid) {
      $this->halt("Invalid SQL: ".$q);
    }
    return $this->qid;
  }
  
  // Return next record in result set
  public function nextRecord() {
    if (!$this->qid) {
      $this->halt("next_record called with no query pending.");
      return 0;
    }
    
    $this->record = mysql_fetch_array($this->qid);
    $this->row   += 1;
    $this->errno  = mysql_errno();
    $this->error  = mysql_error();
    
    $record = is_array($this->record);
    return $record;
  }
 
   // make safe number, ids
  public function makeSafeInt($value){
    $this->connect();
    settype($value, 'integer');
    $value=sprintf("%d",$value);
    return mysql_real_escape_string($value);
}  
  // make safe
  public function makeSafeString($value){
    $this->connect();
   return mysql_real_escape_string($value);
} 
  
  // Field Value
  public function f($field_name) {
    return stripslashes($this->record[$field_name]);
  }
                         
  // Returns the number of rows in query
public  function numberRows() {   
    if ($this->lid) { 
    $this->count=@mysql_num_rows($this->qid);
      return $this->count; //@mysql_num_rows($this->qid); 
    } 
    else { 
      return 0; 
    } 
  }

// Returns the total number of rows in the table
public  function totalRows($table_name) { 
      $this->query("SELECT count(*) AS c FROM $table_name");
      $this->nextRecord();
      return ( $this->f('c') );    
  }
// Returns the id of the last insert
public  function insertID() {   
    if ($this->lid) { 
      return mysql_insert_id($this->lid); 
    } 
    else { 
      return false; 
    } 
  }
  
//Unable to continue, display error message
 private function halt($msg) {
    $this->error = @mysql_error($this->lid);
    $this->errno = @mysql_errno($this->lid);
   if(W3DEBUG) 
   {
      array_push($GLOBALS['W3DEBUG'],'Database Error N&deg;:'.$this->errno.' message: '.$this->error);
	  }
    return false;
  }
}
?>